package org.jspar.model;

import java.util.ArrayList;
import java.util.List;

import org.jspar.route.Expansion;
import org.jspar.route.Trail;


public class Net {

	private String name;
	private Direction type;
	private List<Terminal> terms = new ArrayList<Terminal>();
	
	private int rflag;
	private List<Expansion> expns = new ArrayList<Expansion>();
	private List<Expansion> done = new ArrayList<Expansion>();

	public Net(String name) {
		this.name = name;
		this.type = Direction.NONE;
		this.rflag = 0;
	}
	
	public String name() {
		return name;
	}

	public Terminal[] terminals() {
		return terms.toArray(new Terminal[terms.size()]);
	}
	
	public void addTerminal(Terminal t) {
		terms.add(t);
		t.setNet(this);
	}

	public void setType(Direction type) {
		this.type = type;
	}

	public List<Expansion> expansions() {
		return expns;
	}

	public void addExpansion(Expansion expansion) {
		expns.add(expansion);
	}

	public void setDone(List<Expansion> done) {
		this.done = done;
	}

	public List<Expansion> getDone() {
		return done;
	}

	public int netCost() {
		Expansion ex = (Expansion) done.get(0);
		int cost = 0;
		for (Trail trl : ex.getConnections()) {
			cost += trl.computeCost();
		}
		return (cost);
	}

	public void setStatus(int status) {
		rflag = status;
	}

	public List<Expansion> getExpansions() {
		return expns;
	}

	public boolean isLastExpansionToBeDone() {
		return done.size() + 1 == expns.size();
	}

	public void addDone(Expansion e) {
		done.add(e);
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		boolean isFirst = true;
		builder.append(name).append("(");
		for (Terminal t : terms) {
			if (!isFirst)
				builder.append(", ");
			builder.append(t.toString());
			isFirst = false;
		}
		builder.append(")");
		return builder.toString();
	}

}
